﻿/**
* @file LM35DZ.c
* @brief this is a brief description
* @details this is the detail description.
* @author fulong 
* @data 2015/1/9
* @version 1.0
* @par Copyright (c):
* @par First build
*     version: fulong 
*     data: 2015/1/9
*/
#include "LM35DZ.h"
#include "../bsp/bsp.h"
#include "io_configure.h"
#ifdef __STM32
    #include "../bsp/arch/stm32/stm32f1xx/STM32F10x_StdPeriph_Driver/inc/stm32f10x_adc.h"
    #include "../bsp/arch/stm32/stm32f1xx/STM32F10x_StdPeriph_Driver/inc/stm32f10x_rcc.h"
    #include "../bsp/arch/stm32/stm32f1xx/STM32F10x_StdPeriph_Driver/inc/stm32f10x_gpio.h"
#endif

#define ADC_START()  ADC_SoftwareStartConvCmd(ADC1, ENABLE)
#define ADC_STOP()  ADC_SoftwareStartConvCmd(ADC1, DISABLE)
/*!< 倍数因子，因为电路需要将数据扩大三倍后发给AD */
#define MULTIPLY 3  

LM35DZ_process_struct lm35dz_process;

void lm35dz_init(void)
{
    uint8 i;
    ADC_InitTypeDef  ADC_InitStructure;
    for (i = 0; i < LM35DZ_FILTERING_ARRAY_LEN; i++)
    {
        lm35dz_process.lm35dz_filtering_array[i] = 0;
    }
    lm35dz_process.data_len = 0;
    RCC_ADCCLKConfig(RCC_PCLK2_Div6);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    SET_LM35DZ_INPUT();
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立工作模式
    ADC_InitStructure.ADC_ScanConvMode = DISABLE; //扫描方式
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //连续转换
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //外部触发禁止
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据右对齐
    ADC_InitStructure.ADC_NbrOfChannel = 1; //用于转换的通道数
    ADC_Init(ADC1, &ADC_InitStructure);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_7,  1, ADC_SampleTime_71Cycles5); //通道X,采样时间为1.5周期,1代表规则通道第1个这个1是啥意思我不太清楚只有是1的时候我的ADC才正常。

    /* Enable ADC1 [使能ADC1]*/
    ADC_Cmd(ADC1, ENABLE);

    /* Enable ADC1 reset calibaration register */
    ADC_ResetCalibration(ADC1);
    /* Check the end of ADC1 reset calibration register */
    while (ADC_GetResetCalibrationStatus(ADC1));

    /* Start ADC1 calibaration */
    ADC_StartCalibration(ADC1);
    /* Check the end of ADC1 calibration */
    while (ADC_GetCalibrationStatus(ADC1));
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);
    ADC_START();
}
uint16 get_lm35dz_data(void)
{
    while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
    return (ADC_GetConversionValue(ADC1) / MULTIPLY);
}

